package com.monster.valid.validated;

import com.monster.common.Constant;
import com.monster.exception.impl.MonsterNoTackException;
import com.monster.utils.StringUtils;
import com.monster.valid.annotation.ValidMonster;
import com.monster.valid.common.ValidExceptionEnum;
import com.monster.valid.regex.RegexEnum;
import com.monster.valid.regex.RegexUtils;
import com.monster.valid.utils.ValidCommonUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 字符串校验类
 *
 * @author kuangxz
 */
public class MonsterValidator implements ConstraintValidator<ValidMonster, Object> {
    /**
     * 校验规则
     */
    ValidMonster valid;

    @Override
    public void initialize(ValidMonster constraintAnnotation) {
        valid = constraintAnnotation;
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (ValidCommonUtils.checkNull(valid.isNull(), valid.name(), valid.message(), value)) {
            return true;
        }

        // 替换可输入的特殊字符串
        String specialsMsg = " ";
        String data = String.valueOf(value);
        if (valid.specials() != null) {
            for (String str : valid.specials()) {
                specialsMsg += str;
                data = data.replaceAll(str, "");
            }
        }

        // 枚举校验
        if (valid.value() != null) {
            RegexEnum regexEnum = null;
            for (RegexEnum anEnum : valid.value()) {
                regexEnum = anEnum;
                if (!RegexUtils.isMatch(regexEnum.getPattern(), String.valueOf(value))) {
                    ValidCommonUtils.exception(regexEnum.getCode(), valid.name(), valid.message(), regexEnum.getMessage() + specialsMsg);
                }
            }
        }

        // 自定义正则校验
        if (valid.regex() != null) {
            for (String regex : valid.regex()) {
                if (!RegexUtils.isMatch(regex, String.valueOf(value))) {
                    ValidCommonUtils.exception(Constant.EXCEPTION_CODE, valid.name(), valid.message(), null);
                }
            }
        }

        long min = valid.min(), max = valid.max();
        // 如果其中一个不是默认值的情况下需要校验
        if (valid.toNumber() || min != Long.MIN_VALUE || max != Long.MAX_VALUE) {
            if (value instanceof Integer || value instanceof Long || value instanceof Short) {
                // 大小校验
                long val = Long.parseLong(value.toString());
                if (val < min && val > max) {
                    ValidCommonUtils.exception(ValidExceptionEnum.VALID_NUMBER, valid.name(), valid.message(), "值只能大于" + min + "且小于" + max);
                }
                if (val < min) {
                    ValidCommonUtils.exception(ValidExceptionEnum.VALID_NUMBER, valid.name(), valid.message(), "值只能大于" + min);
                }
                if (val > max) {
                    ValidCommonUtils.exception(ValidExceptionEnum.VALID_NUMBER, valid.name(), valid.message(), "值只能小于" + max);
                }
            } else if (value instanceof String) {
                // 长度校验
                int length = value.toString().length();
                if (length < min && length > max) {
                    ValidCommonUtils.exception(ValidExceptionEnum.VALID_NUMBER, valid.name(), valid.message(), "字符串长度只能大于" + min + "且小于" + max);
                }
                if (length < min) {
                    ValidCommonUtils.exception(ValidExceptionEnum.VALID_NUMBER, valid.name(), valid.message(), "字符串长度只能大于" + min);
                }
                if (length > max) {
                    ValidCommonUtils.exception(ValidExceptionEnum.VALID_NUMBER, valid.name(), valid.message(), "字符串长度只能小于" + max);
                }
            } else {
                throw new MonsterNoTackException(Constant.EXCEPTION_CODE, "规则校验错误");
            }
        }
        return true;
    }

}
